import time
import subprocess


def is_time_synchronized():
    try:
        output = subprocess.check_output(["timedatectl", "status"])
        output = output.decode("utf-8")
        if "System clock synchronized: yes" in output:
            return True
        else:
            return False
    except Exception as e:
        print(e)
        return False


def try_wait_time_sync(timeout):
    while timeout > 0:
        if is_time_synchronized() == True:
            return True
        time.sleep(2)
        timeout -= 2

    return False


print("time sync :", is_time_synchronized())
print("try_wait_time_sync :", try_wait_time_sync(30))
